package com.project.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.RoomBean;
import com.project.bean.StudentBean;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;

public class StudentServiceImp extends BaseService implements IStudentService {
    @Override
    public ArrayList<StudentBean> showAllStudents() {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        ArrayList<StudentBean> allStudents= mapper.showAllStudents();
        session.close();
        return allStudents;
    }

    @Override
    public void studentIn(StudentBean newStudent) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.studentIn(newStudent);
        session.commit();
        session.close();
    }

    @Override
    public void studentOut(Integer stuId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.studentOut(stuId);
        session.commit();
        session.close();
    }

    @Override
    public void studentChange(Integer stuId, Integer newRoomId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.studentChange(stuId,newRoomId);
        session.commit();
        session.close();
    }

    @Override
    public StudentBean studentById(Integer stuId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        StudentBean studentBean = mapper.studentById(stuId);
        session.close();
        return studentBean;
    }

    @Override
    public PageInfo<StudentBean> cutPageByCondition(Integer pageNum, String stuName, String className, String roomAddress) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        PageHelper.startPage(pageNum,2);
        PageInfo<StudentBean> pg =PageInfo.of(mapper.cutPageByCondition(stuName,className,roomAddress));
        session.close();
        return pg;
    }


}
